package org.jypj.dev.controller;

import org.apache.commons.lang.StringUtils;
import org.jypj.dev.model.CodeData;
import org.jypj.dev.model.EmergeBook;
import org.jypj.dev.model.EmergeFile;
import org.jypj.dev.model.Work;
import org.jypj.dev.service.EmergeBookService;
import org.jypj.dev.service.ICodeDataService;
import org.jypj.dev.service.IEmergeFileService;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应急手册
 * Created by ChenYu on 2017-06-27.
 */
@Controller
@RequestMapping(value = "emergeBook")
public class EmergeBookController extends BaseController {

    @Resource
    private EmergeBookService emergeBookService;

    @Resource
    private ICodeDataService codeDataService;

    /**
     * 跳转到应急手册页面
     *
     * @return
     */
    @RequestMapping("toEmergeBook.action")
    public String toEmergeBook(HttpServletRequest request) {
        List<CodeData> codeDatas = codeDataService.selectAllByCode("cxgl");
        request.setAttribute("codeDatas", codeDatas);
        return "back/emerge/emerge_book";
    }

    /**
     * 应急手册搜索
     *
     * @return
     */
    @RequestMapping(value = "toEmergeBookShow.action")
    public String toEmergeBookShow(HttpServletRequest request) {
        List<CodeData> codeDatas = codeDataService.selectAllByCode("cxgl");
        request.setAttribute("codeDatas", codeDatas);
        return "back/emerge/emerge_book_show";
    }

    /**
     * 查询所有数据接口
     *
     * @return code =-1 表示有异常 code=-2 表示没有数据 datas 菜单数据
     */
    @RequestMapping("selectAllBook.action")
    @ResponseBody
    public Map<String, Object> selectAllBook(String trainType) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            queryMap.put("trainType", trainType);
            List<EmergeBook> emergeBooks = emergeBookService.selectAllByMap(queryMap);
            EmergeBook emergeBook = new EmergeBook("0", "应急手册", "");
            emergeBooks.add(emergeBook);
            map.put("data", emergeBooks);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 查询所有数据
     *
     * @param searchKey
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectAll.action")
    public Map<String, Object> selectAll(String searchKey) {
        Map<String, Object> dataMap = new HashMap<>();
        int code = 0;
        try {
            Map<String, Object> queryMap = new HashMap<>();
            queryMap.put("searchKey", searchKey);
            List<EmergeBook> emergeBooks = emergeBookService.selectAllByMap(queryMap);
            dataMap.put("tableData", emergeBooks);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }

    /**
     * 保存或者更新
     *
     * @param emergeBook
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveOrUpdate.action")
    public Map<String, Object> saveOrUpdate(EmergeBook emergeBook) {
        Map<String, Object> dataMap = new HashMap<>();
        int code = 0;
        try {
            // 得到对应的车型并保存
            emergeBook.setCreateTime(DateUtils.getTodayString("yyyy-MM-dd HH:mm:ss"));
            if (StringUtil.isEmpty(emergeBook.getId())) {
                //保存
                emergeBookService.saveEmergeBook(emergeBook);
                dataMap.put("type", "save");
            } else {
                //更新
                emergeBookService.updateEmergeBook(emergeBook);
                dataMap.put("type", "update");
            }
            dataMap.put("eb", emergeBook);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }

        dataMap.put("code", code);
        return dataMap;
    }

    /**
     * 按ID删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteById.action")
    public Map<String, Object> deleteById(String id) {
        Map<String, Object> dataMap = new HashMap<>();
        int code = 0;
        try {
            emergeBookService.deleteEmergeBookById(id);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }

    /**
     * 移动，包括上移和下移
     *
     * @param id1
     * @param id2
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "move.action")
    public Map<String, Object> move(String id1, String id2) {
        Map<String, Object> dataMap = new HashMap<>();
        int code = 0;
        try {
            emergeBookService.moveEmergeBook(id1, id2);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }
}
